/*
 * $Id: TemplateManager.java,v 1.5 2003/05/26 20:46:22 znamenacek Exp $
 *
 * Copyright (c) Milan Jedlicka
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.export;

import java.util.Vector;
import java.io.File;
import java.io.FilenameFilter;

import java.net.URL;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.ListModel;

import org.apache.log4j.Category;

/**
 * Manger of <code>Template</code>s.
 * This manager finds and loads Velocity templates and gives template
 * names to <code>JList</code> for next use.
 *
 * @author Milan Jedlicka
 */
public class TemplateManager implements ListModel {

	/** Logger instance. */
        protected static Category logger = Category.getRoot();
	
	Vector templates = new Vector();
	Vector listeners = new Vector();
	
	/**
	 * Default constructor
	 */
	public TemplateManager() {
		loadTemplates();	
	}

	/**
	 * Finds templates and loads them.
	 */
	public void loadTemplates() {

		// Internal templates
		
		this.templates.add(new PNGExport("PNG")); 
		this.templates.add(new PDFExport("PDF")); 

		String home = System.getProperty("user.home");
                String sep = System.getProperty("file.separator");
		
		// User templates

		File configDir = new File(home + sep + ".vofce/templates");

		if (configDir.exists()) {
			String[] dirUnits;
			dirUnits = configDir.list(new VelocityFilter(".vm"));
			int type = Template.TT_USER;
			
			for (int i = 0; i < dirUnits.length; i++) {
				File templateFile = new File(configDir.getPath() + sep + dirUnits[i]);
				try {		
					this.templates.add(new VelocityTemplate(templateFile, type));
				} catch(Exception e) {
					logger.warn("Error loading template: " + templateFile.getName());
				}
			}
		}
			
		// Vofce templates

		String sourcedir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();

		if (sourcedir.endsWith(".jar")) {
			sourcedir = sourcedir.substring(0, sourcedir.lastIndexOf("/")+1); 
		}

		File path = new File(sourcedir + sep + "templates");

		if (path.exists()) {
			String[] dirUnits;
			dirUnits = path.list(new VelocityFilter(".vm"));
			int type = Template.TT_SYSTEM;
			
			for (int i = 0; i < dirUnits.length; i++) {
				File templateFile = new File(sourcedir + sep + "templates" + sep + dirUnits[i]); 
				
				try {		
				this.templates.add(new VelocityTemplate(templateFile, type));
				} catch(Exception e) {
					logger.warn("Error loading template: " + templateFile.getName());
				}
			
			}
		} else {
			logger.error("TemplateManager: No system templates found.");
			
		}

	}

	/** 
	 * @param   index 	index of template
	 * @return 		template on specific index
	 */
	public Object getTemplateAt(int index) {
		return templates.elementAt(index);
	}
	
	//
	// Methods implementing ListModel
	// 
	
	public Object getElementAt(int index) {
		return templates.elementAt(index);
	}
	
        public int getSize() {
                return templates.size();
        }

	public void addListDataListener(ListDataListener l) {
		listeners.add(l);
	}

	public void removeListDataListener(ListDataListener l) {
		listeners.remove(l);
	}
}

/**
 * Filter for file name.
 * Recognize from name that file is Velocity template
 */
class VelocityFilter implements FilenameFilter {
	String vmFilter;

	public VelocityFilter(String vmFilter) {
		this.vmFilter = vmFilter;
	}

	public boolean accept(File dir, String name) {
		String f = new File(name).getName();
		return f.endsWith(vmFilter);
	}
}
